package org.acooly.secretbox.ui;

import java.util.ArrayList;
import java.util.List;

import org.acooly.android.utils.StringUtils;
import org.acooly.android.utils.ToastUtils;
import org.acooly.secretbox.Constants;
import org.acooly.secretbox.R;
import org.acooly.secretbox.common.BaseActivity;
import org.acooly.secretbox.service.SettingService;
import org.acooly.secretbox.service.UserService;
import org.acooly.secretbox.service.domain.Setting;
import org.acooly.secretbox.service.domain.User;
import org.acooly.secretbox.service.impl.SettingServiceImpl;
import org.acooly.secretbox.service.impl.UserServiceImpl;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class SettingActivity extends BaseActivity {
	ProgressDialog progressDialog = null;
	List<String> listQuestions = null;
	private UserService userService;
	private SettingService settingService;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		userService = new UserServiceImpl(this);
		settingService = new SettingServiceImpl(this);

		// 注册
		Button buttonReigster = (Button) findViewById(R.id.register_submit);
		buttonReigster.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				handleRegister(v);
			}
		});

		final Spinner forgetQuestion = (Spinner) findViewById(R.id.register_forgetQuesture);
		// 获取相应对象
		String[] ls = getResources().getStringArray(R.array.questions);
		listQuestions = new ArrayList<String>();
		// 获取XML中定义的数组
		for (int i = 0; i < ls.length; i++) {
			listQuestions.add(ls[i]);
		}
		ArrayAdapter<String> forgetQuestionType = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listQuestions);
		forgetQuestionType.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
		forgetQuestion.setAdapter(forgetQuestionType);
		forgetQuestion.setSelection(0);

	}

	private void handleRegister(View v) {
		try {
			String password = ((EditText) findViewById(R.id.register_pswd)).getText().toString();
			String confirmPassword = ((EditText) findViewById(R.id.register_confirmPswd)).getText().toString();
			Spinner spinner = (Spinner) findViewById(R.id.register_forgetQuesture);
			String question = listQuestions.get((int) spinner.getSelectedItemId());
			String answer = ((EditText) findViewById(R.id.register_forgetAnswer)).getText().toString();
			if (StringUtils.isBlank(password)) {
				throw new RuntimeException("登录密码不能为空");
			}
			if (StringUtils.isBlank(confirmPassword)) {
				throw new RuntimeException("确认密码不能为空");
			}

			if (!password.equals(confirmPassword)) {
				throw new RuntimeException("两次输入的密码不相同");
			}

			if (StringUtils.isBlank(answer)) {
				throw new RuntimeException("问题答案不能为空");
			}
			User user = new User(password, question, answer, null);
			userService.register(user);
			Setting setting = new Setting(Constants.SETTING_ACCOUNT_TYPE, "默认账户");
			settingService.save(setting);
			SettingActivity.this.finish();
			Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
			startActivity(intent);
		} catch (Exception e) {
			ToastUtils.showShort(getApplicationContext(), e.getMessage());
		}
	}

}